#include "MCGripMover.h"
#include <iostream>

MCGripMover::MCGripMover(ICanvas *presenter) :
    _presenter(presenter),
    _isDraggingValid(false),
    _currentGrip(nullptr)
{
}

void MCGripMover::onMouseDown(IMouseArgs &args)
{
    if (args.isLeftMousePressed())
    {
        _startDraggingPos = args.position();
        _isDraggingValid = true;
        _currentGrip = _presenter->gripAt(args.position());
    }
}

void MCGripMover::onMouseUp(IMouseArgs &args)
{
    _isDraggingValid = false;
    _currentGrip = nullptr;
}

void MCGripMover::onMouseMove(IMouseArgs &args)
{
    if (_isDraggingValid)
    {
        MCPoint2D moveVector = args.position() - _startDraggingPos;
        _startDraggingPos = _startDraggingPos + moveVector;
        moveVector.y *= -1; 
        moveVector = moveVector * _presenter->scale();

        if (_currentGrip != nullptr)
        {
            _currentGrip->move(MCVector2(moveVector));
        }

    }
}
